package com.polaris.live.common.photo.image_preview

import android.app.Activity
import android.app.ActivityOptions
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.Window
import com.google.android.material.transition.platform.MaterialContainerTransform
import com.google.android.material.transition.platform.MaterialContainerTransformSharedElementCallback
import com.polaris.live.common.R
import com.polaris.live.common.databinding.ActivityImagePreviewBinding
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
/**
 * 预览的activity
 *
 * @author Created by 半仙 on 2023/8/15/015 12:01
 */
class ImagePreviewActivity : BaseVbActivity<BaseViewModel, ActivityImagePreviewBinding>() {

    private var mAdapter: ImageVpAdapter? = null

    override fun initView(savedInstanceState: Bundle?) {
        // 只有安卓版本大于 5.0 才可使用过度动画
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !TextUtils.isEmpty(ImagePreview.instance.transitionShareElementName)) {
            window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
            findViewById<View>(android.R.id.content).transitionName = "shared_element_container"
            setEnterSharedElementCallback(MaterialContainerTransformSharedElementCallback())
            window.sharedElementEnterTransition = MaterialContainerTransform().addTarget(android.R.id.content).setDuration(300L)
            window.sharedElementReturnTransition = MaterialContainerTransform().addTarget(android.R.id.content).setDuration(250L)
        }

        mAdapter = ImageVpAdapter(this, ImagePreview.instance.getImageInfoList())
        mBinding.imageViewPage.adapter = mAdapter
    }

    companion object {
        fun activityStart(context: Context) {
            val intent = Intent()
            intent.setClass(context, ImagePreviewActivity::class.java)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //过度动画效果只有5.0版本以上有效果
                val transitionView = ImagePreview.instance.transitionView
                val transitionShareElementName = ImagePreview.instance.transitionShareElementName
                //如果未设置则使用默认动画
                if (transitionView != null && transitionShareElementName != null) {
                    val options = ActivityOptions.makeSceneTransitionAnimation(
                        context as Activity?,
                        transitionView,
                        transitionShareElementName
                    )
                    context.startActivity(intent, options.toBundle())
                } else {
                    context.startActivity(intent)
                    if (context is Activity) {
                        context.overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
                    }
                }
            } else {
                //低于5.0 默认动画
                context.startActivity(intent)
                if (context is Activity) {
                    context.overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
                }
            }
        }
    }
}